package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/system/service"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"boy-go/pkg/response"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"strings"
)

// AddSysNoticeRouter 添加路由
func AddSysNoticeRouter(group *gin.RouterGroup) {
	group.GET("/notice/:id", middlewares.HasPermission("system:notice:query"), get_notice)
	group.GET("/notice/list", middlewares.HasPermission("system:notice:list"), list_notice)
	group.POST("/notice", middlewares.HasPermission("system:notice:add"), middlewares.Logger("通知公告表", middlewares.INSERT), add_notice)
	group.PUT("/notice", middlewares.HasPermission("system:notice:edit"), middlewares.Logger("通知公告表", middlewares.UPDATE), edit_notice)
	group.DELETE("/notice/:ids", middlewares.HasPermission("system:notice:remove"), middlewares.Logger("通知公告表", middlewares.DELETE), del_notice)
}

// get_notice
// @Summary 获取通知公告表详情
// @Description 获取通知公告表详情（管理页面）
// @Tags system-notice
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.SysNoticeModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/notice/{id} [get]
func get_notice(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewSysNoticeService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_notice
// @Summary 获取通知公告表列表
// @Description 获取通知公告表列表（管理页面）
// @Tags system-notice
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.SysNoticeModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/notice/list [get]
func list_notice(ctx *gin.Context) {
	req := vo.QuerySysNoticeReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	page, err := service.NewSysNoticeService().List(&req)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxDataTable(ctx, page.List, page.Total)
}

// add_notice
// @Summary 新增通知公告表
// @Description 新增通知公告表（管理页面）
// @Tags system-notice
// @Accept json
// @Produce json
// @Param req body  vo.AddSysNoticeReq true "req"
// @Success 200 {object} response.R[vo.AddSysNoticeRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/notice [post]
func add_notice(ctx *gin.Context) {
	var req vo.AddSysNoticeReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysNoticeService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_notice
// @Summary 编辑通知公告表
// @Description 编辑通知公告表（管理页面）
// @Tags system-notice
// @Accept json
// @Produce json
// @Param req body  vo.EditSysNoticeReq true "req"
// @Success 200 {object} response.R[vo.EditSysNoticeRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/notice [put]
func edit_notice(ctx *gin.Context) {
	var req vo.EditSysNoticeReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysNoticeService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_notice
// @Summary 删除通知公告表
// @Description 删除通知公告表（管理页面）
// @Tags system-notice
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/notice [delete]
func del_notice(ctx *gin.Context) {
	var idsStr = ctx.Param("ids")
	ids := strings.Split(idsStr, ",")
	err := service.NewSysNoticeService().BatchDel(ids)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, nil)
	}
}
